//
//  XMGBrowserCell.m
//  微博
//
//  Created by zxy on 2018/12/26.
//  Copyright © 2018 zxy. All rights reserved.
//

#import "XMGBrowserCell.h"
@interface XMGBrowserCell()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *browserScrollView;
@end
@implementation XMGBrowserCell
/**利用scrollview实现图片上下滑动和缩放*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.browserScrollView];
        [self.browserScrollView addSubview:self.browserImageView];
    }
    return self;
}

-(UIScrollView *)browserScrollView{
    if(!_browserScrollView){
        _browserScrollView = [[UIScrollView alloc]initWithFrame:self.contentView.bounds];
        _browserScrollView.pagingEnabled = YES;
        _browserScrollView.bounces = NO;
        _browserScrollView.showsVerticalScrollIndicator = NO;
        _browserScrollView.delegate = self;
        /** 设置最小最大缩放尺寸 */
        _browserScrollView.minimumZoomScale = 0.5;
        _browserScrollView.maximumZoomScale = 2;
    }
    return _browserScrollView;
}

-(UIImageView *)browserImageView{
    if(!_browserImageView){
        _browserImageView = [[UIImageView alloc]init];
        [_browserImageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(close)]];
        _browserImageView.userInteractionEnabled = YES;
    }
    return _browserImageView;
}

-(void)close{
    if(_delegate && [_delegate respondsToSelector:@selector(pictureBrowserDissmiss)]){
        [_delegate pictureBrowserDissmiss];
    }
}

/**
 重置scrollview和imageview的属性，不然第一张图片放大，后面图片可以看不到了
 */
-(void)reset{
    self.browserScrollView.contentInset = UIEdgeInsetsZero;
    self.browserScrollView.contentOffset = CGPointZero;
    self.browserScrollView.contentSize = CGSizeZero;
    // 重置imageview,图片缩放使用的是transform，所以也要重置
    self.browserImageView.transform = CGAffineTransformIdentity;
}

-(void)setPictureUrl:(NSString *)pictureUrl{
    _pictureUrl = pictureUrl;
     [self reset];
    //在这里不会造成循环引用问题
    [self.browserImageView sd_setImageWithURL:[NSURL URLWithString:pictureUrl] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        //获取图片真实宽高比
        CGFloat p = image.size.height / image.size.width;
        //获取图片高度
        CGFloat height = p * SCREEN_WIDTH;
        CGFloat y = 0;
        if(height >=SCREEN_HEIGHT){//图片高度大于屏幕高度，从0显示   //长图
            y = 0;
            //长途
            self.browserScrollView.contentSize = CGSizeMake(0, height);
        }else{
            //设置如果图片高度小于屏幕高度居中显示
            y = (SCREEN_HEIGHT - height) * 0.5;
        }
        self.browserImageView.frame = CGRectMake(0, y, SCREEN_WIDTH, height);
    }];
}

-(void)setPictureArrayUrl:(NSArray *)pictureArrayUrl{
    _pictureArrayUrl = pictureArrayUrl;
    for (NSInteger i = 0; i < pictureArrayUrl.count; i++) {
        NSString *url = pictureArrayUrl[i];
        UIImageView *browserImageView = [[UIImageView alloc]init];
        [self.browserScrollView addSubview:browserImageView];
        [browserImageView sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            //获取图片真实宽高比
            CGFloat p = image.size.height / image.size.width;
            //获取图片高度
            CGFloat height = p * SCREEN_WIDTH;
            CGFloat y = 0;
            if(height >=SCREEN_HEIGHT){//图片高度大于屏幕高度，从0显示   //长图
                y = 0;
                //长途
//               self.browserScrollView.contentSize = CGSizeMake(0, height);
            }else{
                //设置如果图片高度小于屏幕高度居中显示
                y = (SCREEN_HEIGHT - height) * 0.5;
            }
            browserImageView.frame = CGRectMake(i * SCREEN_WIDTH, y, SCREEN_WIDTH, height);
        }];
    }
    self.browserScrollView.contentSize = CGSizeMake(pictureArrayUrl.count * SCREEN_WIDTH, 0);
//  [self.browserScrollView setContentOffset:CGPointMake(_index * SCREEN_WIDTH, 0) animated:NO];
}

//要缩放的view
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return self.browserImageView;
}

//已经结束缩放
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale{
    //view：缩放的view
    CGFloat offsetX = (self.bounds.size.width - view.frame.size.width)*0.5;
    CGFloat offsetY = (self.bounds.size.height - view.frame.size.height)*0.5;
    offsetX = offsetX < 0 ? 0 : offsetX;
    offsetY = offsetY < 0 ? 0 : offsetY;
    view.frame = CGRectMake(offsetX, offsetY, view.frame.size.width, view.frame.size.height);
}

@end
